package zfc.D0911;

import java.util.*;

public class P1 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        // 状态数据
        int N = Integer.parseInt(scanner.nextLine());
        Map<Integer, List<Integer>> treeMap = new HashMap<>();
        for (int i = 0; i < N; i++) {
            String[] params = scanner.nextLine().split("\\s+");
            List<Integer> list = new ArrayList<>();
            for (int j = 1; j < params.length; j++) {
                list.add(Integer.parseInt(params[j]));
            }
            treeMap.put(i, list);
        }
        // 求解
        System.out.println(postScan(treeMap, 0));
    }

    private static int postScan(Map<Integer, List<Integer>> treeMap, int pos) {
        List<Integer> row = treeMap.get(pos);
        int max = 0;
        for (int i = 1; i < row.size(); i++) {
            int tmp = postScan(treeMap, row.get(i));
            max = Math.max(tmp, max);
        }
        return max + row.get(0);
    }
}
